//index.js
const app = getApp()
const { $Message } = require('../../dist/base/index');
const imgBackUrlOriginal="cloud://ldd-pgj55.6c64-ldd-pgj55-1304519390/backP.png"//登录界面原图
const imgTitleUrlOriginal="cloud://ldd-pgj55.6c64-ldd-pgj55-1304519390/1.jpg"//初始界面风景图原图
const imgButtonUrlOriginal="cloud://ldd-pgj55.6c64-ldd-pgj55-1304519390/initpintu_副本1.jpg"//初始界面按钮原图
Page({
  data: {

    logged: false,
    takeSession: false,
    current: 'homepage',
    requestResult: '',
    hasUserInfo: app.globalData.hasUserInfo,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    showRight1: false,//是否展示使用说明
    messages:[],//留言信息
    heightPage:0,//三个主要页面的高度
    color:["#009966","#999999","#FF9900","#999966","#7C6600","#FF69B4","#3F3CCC","#FF99CC","#990066"],//颜色的数组集合
    imgBackUrl:"../../images/back.png",//背景图片
    imgTitleUrl:"../../images/title.jpg",//开头风景图片文件
    imgButtonUrl:"../../images/button.jpg",//开头按钮图片
    imgLoadList:[]//初始化空的高清图列表
  },
  //底部tabbar的切换
  handleChange ({ detail }) {
    if(detail.key==="barrage"){
      wx.navigateTo({
        url: '../duty/duty',
      })
    }
    else if(detail.key==="mine"){
      wx.navigateTo({
        url: '../own/own',
      })

    }
   
},
//点击打开使用说明
  toggleRight1() {
  this.setData({
      showRight1: !this.data.showRight1
  });
},

//获取用户信息
  getUserInfo: function(e) {
  app.globalData.userInfo = e.detail.userInfo
  if(app.globalData.userInfo){
    this.setData({
      hasUserInfo:true
    })
    //设置缓存过期时间
    // var timestamp=Date.parse(new Date())
    // timestamp=timestamp/1000
    // let overtimestamp=timestamp+86400*5
    // wx.setStorageSync("overtime ", overtimestamp);
    //将标志位写入缓存，在app.js中读取缓存
    wx.setStorageSync('hasUserInfo', this.data.hasUserInfo)
    app.globalData.hasUserInfo=true
  }
  else{
    wx.showToast({
      title: '授权失败,请重试',
      icon:"none",
      duration:800,
      mask:true
    })
  }
},
//点击发布信息，跳转到填信息界面
publish:function(){
  wx.navigateTo({
    url: '../publishing/publishing',
  })
},
//加载图片完后，将实际模糊图替换为高清图，并删除该图
imgOnLoad(e){
  const src=e.currentTarget.dataset.src
  const index=e.currentTarget.dataset.index
  switch(index){
    case 0:
      this.setData({imgBackUrl:src})
    break
    case 1:
      this.setData({imgTitleUrl:src})
    break
    case 2:
      this.setData({imgButtonUrl:src})
    break
  }
  this.index++
  if(this.index>=this.data.imgLoadList.length){
    this.setData({
      imgLoadList:[]
    })
  }
},
//高清图加载失败
imgOnLoadError(){
  $Message({
    content: '高清图加载失败',
    type: 'error'
});
},
  onLoad: function() {
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              app.globalData.userInfo=res.userInfo
            }
          })
        }
      }
    })
    wx.getSystemInfo({
      success: (result) => {
        this.setData({
          heightPage:result.windowHeight
        })
      },
    })
    wx.cloud.callFunction({
      name: 'login',
      data: {},
      success: res => {
        console.log('[云函数] [login] user openid: ', res.result.openid)
        app.globalData.openid = res.result.openid
      },
      fail: err => {
        wx.navigateTo({
          url: '../index/index',
        })
      }
    })
    if (!wx.cloud) {
      wx.redirectTo({
        url: '../chooseLib/chooseLib',
      })
      return
    }

  },
  onShow:function(){
    //加载匿名留言信息
    var db=app.globalData.db
    db.collection('message').orderBy('datetime','desc').get({
      success:res=>{
        this.setData({
          messages:res.data
        })
      },
      fail:err=>{
          $Message({
            content: '数据加载失败',
            type: 'error'
        });
      }
    })

  
  },
 
onReady(){
  this.index=0
  //加载完成后模糊图全部替换为原图
  this.setData({
    imgLoadList:[imgBackUrlOriginal,imgTitleUrlOriginal,imgButtonUrlOriginal]
  })

},
})
